<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\model\jeek\Product as ProductModel;
use app\common\model\jeek\products\Price;
use app\common\model\Admin;
/**
 * 首页接口
 */
class product extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    public function getlistData()
    {
                // $params=input('params.');

                $page=input('params.page',1);
                $limit=input('params.limit',15);
                $pid=input('params.pid',0);
                $keyword=input('params.keyword','');
                $order=input('params.order','desc');


                $auid = (int)input('params.auid', 0);
                
                if($auid===0){
                    // $where['auid'] = $auid;
                   $auid=1;
                }else{
                      // 获取隐藏的产品分类ID
    $hiddenCategories = Db::name('jeek_products_hide')->where('admin_id', $auid)->column('product_id');
    // dump($hiddenCategories);die();
    // 如果有隐藏的产品分类，则添加到查询条件中
    if (!empty($hiddenCategories)) {
        $where['id'] = [ 'not in', $hiddenCategories];
    }

                }

                $defaultAdmin = Admin::name('admin')->where('id', $auid)->find();
                if ($defaultAdmin) {
                    if($defaultAdmin['gy_lat']){
                        $gy_lat = $defaultAdmin['gy_lat'];
                    }else{
                        $gy_lat = 1; // 默认值为1
                    }

                } else {
                    $this->error('数据出错');
                }


                if($keyword){
                    $where['title|description'] = ['like', "%$keyword%"];
                    
                    if(intval($pid)===146){
                        $where['jeek_category_id'] = intval($pid);
                    }
                    // dump($where);die();
                }else{
                    $where['jeek_category_id'] = intval($pid);
                }
        $cdnUrl = config('cdn_url');
        $where['status'] = 1;
        // $where['jeek_category_id'] = intval($pid);
        if($auid===1){
        $where['admin_id'] = $auid;
    }else{
        $where['admin_id'] = ['in',[1,  $auid]];
    }


// 打印查询条件
// print_r($where);
if($order){
    $data=ProductModel::where($where)->order('price '.$order)->order('weigh desc')->order('id desc')->paginate($limit,true,['page'=>$page]);
}else{
   
    $data=ProductModel::where($where)->order('weigh desc')->order('id desc')->paginate($limit,true,['page'=>$page]);
}
      
// if($data->hasNextPage()){
//     $hasMore=true;
// }else{
//     $hasMore=false;
// }

        foreach ($data as $key => $value) {
            $value['price'] = $value['price'] * $gy_lat;
            $data[$key]['image']=$cdnUrl.$value['image'];
        }
// dump($data['hasMore']);dump($data);die();
        return json([
            'code' => 1,
            'msg' => 'success',
            'data' => $data
        ]);
        // $this->success('请求成功', $data);
    }


 /**
     * 获取产品详情
     *
     * @return \think\Response
     */
    public function getDetailData()
    {
        $id = intval(input('params.id', 0));
        $uid = intval(input('params.uid', 1));
    // 根据 uid 获取 admin 模型的 gy_lat 字段值
    $admin = Admin::where('id', $uid)->find();
    
    if ($admin) {
        $gy_lat = $admin['gy_lat'];
     
    } else {
       $gy_lat=1;
    }

        if ($id <= 0) {
            return json([
                'code' => 0,
                'msg' => '无效的产品ID',
                'data' => null
            ]);
        }

        $product = ProductModel::where('id', $id)->find();

        if (!$product) {
            return json([
                'code' => 0,
                'msg' => '产品不存在',
                'data' => null
            ]);
        }

        $product['taocan']=Price::where('product_id',$id)->order('id desc')->select();
   
        // 假设 $product['taocan'] 是一个数组，包含多个产品信息
        foreach ($product['taocan'] as $key=>$taocan) {
            // 计算新的价格
            $taocan['price'] = $taocan['price'] * $gy_lat;
        // return $taocan;
        }
        
        $cdnUrl = config('cdn_url');
        $product['image'] = $cdnUrl . $product['image'];
        if ($product['video_url']) {
            $product['video_url']=$cdnUrl . $product['video_url'];
        }
        // $product['video_url']=$cdnUrl . $product['video_url'];

        if ($product['images']) {
            $imags = explode(',', $product['images']);
            foreach ($imags as $key => $value) {
                $imags[$key] = $cdnUrl . $value;
            }   
            $product['images']  = $imags;
        }

        return json([
            'code' => 1,
            'msg' => 'success',
            'data' => $product
        ]);
    }

}
